package cn.b.sky.user.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.enums.DwzCallbackType;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.enums.ECodeEnums;
import cn.b.sky.user.enums.ResourceTypeCode;
import cn.b.sky.user.service.MenuService;
import cn.b.sky.user.service.ResourceService;
import cn.b.sky.user.service.model.Menu;
import cn.b.sky.user.service.model.ResourceEntity;
import cn.b.sky.user.service.model.condition.MenuCondition;
import cn.b.sky.user.service.model.condition.ResourceCondition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 
 * @ClassName: MenuController
 * @Description:菜单管理控制器
 * @author: ZhaoXin
 * @date: 2015-7-28 上午11:13:04
 * @version: V1.0
 * 
 */
@Controller
@RequestMapping(value = "/menu/*")
public class MenuController extends SecurityController {

  @Resource
  private MenuService menuService;

  @Resource
  private ResourceService resourceService;

  @RequestMapping("menuList")
  public String menuList(Model model, String navTabId, String selectNode) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("selectNode", selectNode);
    List<TreeInterface> menus = menuService.selectMenuList(getSiteCode(),true);

    model.addAttribute("treeList", menus);
    return "menu/menu_list";
  }

  @RequestMapping("addMenu")
  public String addMenu(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    List<ResourceEntity> resources = resourceService.selectAllUnLockResource();
    model.addAttribute("resources", resources);
    if (!StringUtils.isBlank(id)) {
      Menu menu = menuService.selectMenuByCode(getSiteCode(),id);
      Menu m = new Menu();
      m.setParentMenuCode(id);
      m.setParentMenuText(menu.getMenuText());
      model.addAttribute("menu", m);
    }
    return "menu/menu_add";
  }

  @RequestMapping("validateMenuCode")
  @ResponseBody
  public boolean validateMenuCode(String menuCode, Model model) {
    if (!StringUtils.isBlank(menuCode)) {
      return null == menuService.selectMenuByCode(getSiteCode(),menuCode);
    }
    return true;

  }

  @RequestMapping("saveMenu")
  @ResponseBody
  public DWZResult saveMenu(Model model, String navTabId, String id, Menu menu, HttpServletRequest request) {
    if (menu == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    menu.setSiteCode(getSiteCode());
    setCreateInfo(menu);
    if (StringUtils.isEmpty(id)) {
      int ord = 0;
      if (StringUtils.isEmpty(menu.getParentMenuCode())) {
        ord = menuService.selectRootMaxOrdinal(getSiteCode()) + 1;
      } else {
        ord = menuService.selectMaxOrdinal(getSiteCode(),menu.getParentMenuCode()) + 1;
      }
      menu.setDisplayOrdinal(ord);
      menuService.insertMenu(menu);
    } else {
      menuService.updateMenu(menu);
    }
    String forwardUrl = getForwardUrl(request) + "/menu/menuList?navTabId=" + navTabId + "&selectNode="
        + menu.getMenuCode();
    return navTabAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, DwzCallbackType.FORWARD, forwardUrl, null, null);

  }

  @RequestMapping("editMenu")
  public String editMenu(Model model, String navTabId, String id) {
    Menu menu = menuService.selectMenuByCode(getSiteCode(),id);
    if (menu != null && !StringUtils.isEmpty(menu.getParentMenuCode())) {
      Menu pmenu = menuService.selectMenuByCode(getSiteCode(),menu.getParentMenuCode());
      menu.setParentMenuText(pmenu.getMenuText());
    }
    model.addAttribute(NAVTABID, navTabId);
    if (menu != null) {
      ResourceEntity resource = resourceService.selectResource(menu.getResourceCode());
      menu.setResourceText(resource.getResourceText());
    }
    model.addAttribute("pattern", "modify");
    model.addAttribute("menu", menu);
    return "menu/menu_add";

  }

  @RequestMapping("viewMenu")
  public String viewMenu(Model model, String navTabId, String id) {
    Menu menu = menuService.selectMenuByCode(getSiteCode(),id);
    if (menu != null && !StringUtils.isEmpty(menu.getParentMenuCode())) {
      Menu pmenu = menuService.selectMenuByCode(getSiteCode(),menu.getParentMenuCode());
      menu.setParentMenuText(pmenu.getMenuText());
    }
    model.addAttribute(NAVTABID, navTabId);
    if (menu != null) {
      ResourceEntity resource = resourceService.selectResource(menu.getResourceCode());
      menu.setResourceText(resource.getResourceText());
    }
    model.addAttribute("menu", menu);
    return "menu/menu_view";

  }

  @RequestMapping("deleteMenu")
  @ResponseBody
  public DWZResult deleteMenu(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    List<Menu> mlist = menuService.selectMenuByParent(getSiteCode(),id, true);
    if (mlist != null && mlist.size() > 0) {
      return ajaxError(
          getImeViewMessageI18n(null, "cannotDelMenu.M"));
    }
    menuService.deleteMenu(getSiteCode(),id);
    return navTabAjaxDone(navTabId, null, null, null,
        getImeViewMessageI18n(null, "successfulOperation.M"),
        null);

  }

  @RequestMapping("upMenu")
  @ResponseBody
  public DWZResult upMenu(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    Menu menu = menuService.selectMenuByCode(getSiteCode(),id);
    List<Menu> mlist;
    if (StringUtils.isEmpty(menu.getParentMenuCode())) {
      mlist = menuService.selectRootMenu(getSiteCode(),true);
    } else {
      mlist = menuService.selectMenuByParent(getSiteCode(),menu.getParentMenuCode(), true);
    }
    if (mlist.size() == 1) {
      return ajaxError(
          getImeViewMessageI18n(null, "cannotUpMenu.M"));
    }
    for (int i = 0; i < mlist.size(); i++) {
      if (mlist.get(i).getMenuCode().equals(id)) {
        if (i == 0) {
          String s = getImeViewMessageI18n(null,
              "cannotUpMenu.M");
          return ajaxError(s);
        } else {
          Menu menuUp = mlist.get(i - 1);
          int ord = menu.getDisplayOrdinal();
          menu.setDisplayOrdinal(menuUp.getDisplayOrdinal());
          menuUp.setDisplayOrdinal(ord);
          menuService.updateMenu(menu);
          menuService.updateMenu(menuUp);

        }
      }
    }
    String forwardUrl = getForwardUrl(request) + "/menu/menuList?navTabId=" + navTabId + "&selectNode="
        + menu.getMenuCode();
    return navTabAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, DwzCallbackType.FORWARD, forwardUrl, null, null);

  }

  @RequestMapping("downMenu")
  @ResponseBody
  public DWZResult downMenu(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    Menu menu = menuService.selectMenuByCode(getSiteCode(),id);
    List<Menu> mlist;
    if (StringUtils.isEmpty(menu.getParentMenuCode())) {
      mlist = menuService.selectRootMenu(getSiteCode(),true);
    } else {
      mlist = menuService.selectMenuByParent(getSiteCode(),menu.getParentMenuCode(), true);
    }
    if (mlist.size() == 1) {
      return ajaxError(
          getImeViewMessageI18n(null, "cannotDownMenu.M"));
    }
    for (int i = 0; i < mlist.size(); i++) {
      if (mlist.get(i).getMenuCode().equals(id)) {
        if (i == mlist.size() - 1) {
          return ajaxError(
              getImeViewMessageI18n(null, "cannotDownMenu.M"));
        } else {
          Menu menuUp = mlist.get(i + 1);
          int ord = menu.getDisplayOrdinal();
          menu.setDisplayOrdinal(menuUp.getDisplayOrdinal());
          menuUp.setDisplayOrdinal(ord);
          menuService.updateMenu(menu);
          menuService.updateMenu(menuUp);

        }
      }
    }
    String forwardUrl = getForwardUrl(request) + "/menu/menuList?navTabId=" + navTabId + "&selectNode="
        + menu.getMenuCode();
    return navTabAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, DwzCallbackType.FORWARD, forwardUrl, null, null);

  }

  @RequestMapping("closeMenu")
  @ResponseBody
  public DWZResult closeMenu(Model model, String navTabId, HttpServletRequest request) {
    String forwardUrl = getForwardUrl(request) + "/menu/menuList?navTabId=" + navTabId;
    return navTabAjaxDone(null, "menuList", null, DwzCallbackType.FORWARD, forwardUrl, null, null);
  }

  @RequestMapping("selectResource")
  public String selectResource(Model model, @ModelAttribute("condition") ResourceCondition resource, String navTabId) {
    resource.setLanguageCode(SecurityContextUtil.getLanguageCode());
    resource.seteCodeType(ECodeEnums.RESOURCETYPEECODE.getCode());
    resource.setLockFlag(0);
    List<ResourceEntity> resources = resourceService.selectResourceList(resource);
    int count = resourceService.selectResourceCount(resource);
    resource.setData(resources);
    resource.setTotalCount(count);
    model.addAttribute("page", resource);
    model.addAttribute(NAVTABID, navTabId);
    return "menu/resource_select";
  }
  
  @RequestMapping("menuLookUp")
  public String menuLookUp(Model model, @ModelAttribute("condition") MenuCondition condition, String navTabId) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    condition.setResourceTypeECode(ResourceTypeCode.MODULE.getValue());
    List<Menu> menus = menuService.selectTypeMenuListByCondition(condition);
    int count = menuService.selectTypeMenuCountByCondition(condition);
    condition.setData(menus);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "menu/menu_select";
  }
}
